#!/usr/bin/env python
# encoding: utf-8
'''
@author: aliax
@license: (C) Copyright 2018-2020.
@contact: 1048327635@qq.com
@file: api.py
@time: 2020/7/9 0009 23:25
@desc:
'''
from flask import Flask
import Config.db as db
from Config.config import host_ip,host_port
import random

app = Flask(__name__)

text = """
/get         随机获取一个可用的代理IP<br>
/status      查看当前代理采集和检测情况<br><br><br>
这几个免费网站上可用的太少了，等我找找国外的代理网站试试。
"""

@app.route('/')
def index():
    return text
@app.route('/get')

def get():
    useful = db.keys('useful')
    return random.choice(useful).decode('utf-8')
@app.route('/status')

def status():
    count_all = db.len("all")
    count_use = db.len('useful')
    text = '代理总数：'+str(count_all)+'  <br>可用数量：'+str(count_use)
    return text

def runFlask():
    '''
    cli工具调用，启动api接口
    :return:
    '''
    app.run(host=host_ip, port=host_port)

